package com.sc.rest.service.impl;

import com.sc.common.utils.JsonUtils;
import com.sc.mapper.TbItemDescMapper;
import com.sc.mapper.TbItemMapper;
import com.sc.mapper.TbItemParamItemMapper;
import com.sc.mapper.TbItemParamMapper;
import com.sc.pojo.*;
import com.sc.rest.component.JedisClient;
import com.sc.rest.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther sc
 * @Date 2017/10/27
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Value("${REDIS_ITEM_KEY}")
    private String REDIS_ITEM_KEY;
    @Value("${ITEM_BASE_INFO_KEY}")
    private String ITEM_BASE_INFO_KEY;
    @Value("${EXPIRE_ITEM}")
    private Integer EXPIRE_ITEM;
    @Value("${ITEM_DESC_KEY}")
    private String  ITEM_DESC;
    @Value("${ITEM_PARAM_KEY}")
    private String  ITEM_PARAM_KEY;

    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private  TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    @Autowired
    private JedisClient jedisClient;


    /***
     * 查询商品
     * redis查询不能影响业务的执行
     * @param itemId
     * @return item Object
     */
    public TbItem getItemById(Long itemId) {
        //查缓存
        try {
            String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_BASE_INFO_KEY);
            if(StringUtils.isNotBlank(json)){
               return JsonUtils.jsonToPojo(json,TbItem.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        //写缓存
        try {
            jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_BASE_INFO_KEY,JsonUtils.objectToJson(tbItem));
            jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_BASE_INFO_KEY,EXPIRE_ITEM);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbItem;
    }

    /***
     * 查询商品详情
     * @param itemId 主键
     * @return
     */
    public TbItemDesc getItemDesc(Long itemId) {
        //查缓存
        try {
            String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC);
            if(StringUtils.isNotBlank(json)){
                return JsonUtils.jsonToPojo(json,TbItemDesc.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        //写缓存
        try {
            jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC,JsonUtils.objectToJson(tbItemDesc));
            jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC,EXPIRE_ITEM);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbItemDesc;
    }


    public TbItemParamItem getItemParam(Long itemId){
        //查缓存
        try {
            String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY);
            if(StringUtils.isNotBlank(json)){
                return JsonUtils.jsonToPojo(json,TbItemParamItem.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbItemParamItemExample ex = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = ex.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(ex);
        if(list!=null&&list.size()>0){
            TbItemParamItem item = list.get(0);
            //写缓存
            try {
                jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY,JsonUtils.objectToJson(item));
                jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY,EXPIRE_ITEM);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return item;
        }
        return null;
    }
}
